package main;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;


public class WAVPlayer {

	AudioInputStream audio = AudioSystem.getAudioInputStream(new File("C:\\Users\\Jan\\a.wav"));
    Clip clip;
    SourceDataLine soundLine = null;
    int BUFFER_SIZE = 64*1024;  // 64 KB
    private String filename;
    Player player;
    


	public WAVPlayer(File soundFile) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
		try {
			System.out.println(soundFile.getAbsolutePath());
	     //    URL url = this.getClass().getClassLoader().getResource();
	         MediaLocator locator = new MediaLocator(soundFile.toURI().toURL());
	         player = Manager.createPlayer(locator);
	         player.addControllerListener(new ControllerListener() {
	            public void controllerUpdate(ControllerEvent event) {
	               if (event instanceof EndOfMediaEvent) {
	                  player.stop();
	                  player.close();
	               }
	            }
	         });
	         player.realize();
	         player.start();
	      } catch (Exception e) {
	         e.printStackTrace();
	      }
	}
	
	public void play() {
		//clip.start();
	}
	
	public void stop() {
		clip.stop();
	}
	
	public void seek(int percentage) {
		boolean isRunning = clip.isRunning();
		 
		System.out.println(clip.getMicrosecondLength()    );
		clip.setMicrosecondPosition((long)(clip.getMicrosecondLength() * percentage /100.0)); 
		if(isRunning) 
			clip.start();
	}
}
